package com.example.appxi;

import com.navdrawer.SimpleSideDrawer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Interpolator;

class SuperSimpleSlideDrawer extends SimpleSideDrawer implements OnClickListener
{
	
	
	boolean slideMenuisOpen;
	public boolean isOpen;
	Button btn_search;
	EditText searchField;
	ParentActivity parentActivity;
	public SuperSimpleSlideDrawer(Activity act, Interpolator ip, int duration) {
		super(act, ip, duration);
		// TODO Auto-generated constructor stub

	}

	public SuperSimpleSlideDrawer(ParentActivity parentActivity) {
		// TODO Auto-generated constructor stub
		super(parentActivity);
		this.parentActivity=parentActivity;
		setRightBehindContentView(R.layout.slidemenu);
		btn_search = (Button) findViewById(R.id.btn_search);
		searchField=(EditText) findViewById(R.id.Edit1);
		slideMenuisOpen=false;
		searchField.setEnabled(false);
		searchField.setClickable(false);
	}
	@Override
	public void close()
	{
		this.setClickable(false);
		super.close();
	}
	
	@Override
	public void closeLeftSide()
	{
		this.setClickable(false);
		super.closeLeftSide();
	}
	@Override
	public void closeRightSide()
	{
		this.setClickable(false);
		searchField.setEnabled(true);
		searchField.setClickable(true);
		btn_search.setOnClickListener(null);
		super.closeRightSide();
	}
	public void openMenu()
	{
		isOpen=true;
		btn_search.setOnClickListener(parentActivity.OCL_slideMenu_Search);
		searchField.setEnabled(true);
		searchField.setClickable(true);
		toggleRightDrawer();
	}
	
	public void REopenMenu()
	{
		getRightBehindView().setVisibility(getRightBehindView().VISIBLE);
		toggleRightDrawer();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
}
public class ParentActivity extends ActionBarActivity implements View.OnClickListener
{

	Button btn_searchPlace;
	View btn_banner;
	Button btn_friChat;
	Button btn_slidesearch;
	View btn_home;
	Button btn_leftSlideMenu;
	Button btn_myfriend;
	Button btn_myplace;
	View btnRightSlide;
	Button btn_search;
	AlertDialog.Builder aDialog;
	LayoutInflater inflater;
	EditText searchField;
	boolean slideMenuisOpen=false;
	
	OnClickListener OCL_slideMenu;
	OnClickListener OCL_slideMenu_Search;
	
	public Activity top;
	private SuperSimpleSlideDrawer mNav;
	private BackPressCloseHandler backPressCloseHandler;
	
	private int heightS;
	private int heightA;
	
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        // 액션바를 가져왔다
     	android.app.ActionBar bar = getActionBar();
     	// 뷰를 가져옴
     	View v = getLayoutInflater().inflate(R.layout.custombar, null);
     	// 액션바에 커스텀뷰를 설정
     	bar.setCustomView(v);
     	// 커스텀 뷰를 써야하므로 옵션에서 설정
     	bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
     	

     	LinearLayout layout = new LinearLayout(this);
     	
        layout.post(new Runnable() {
            @Override
            public void run() {
                getStatusBarSize();
            }
        });
        

        
     	//findViewById(R.id.slide).setVisibility(View.GONE);
        mNav = new SuperSimpleSlideDrawer(this);
        //mNav.setRightBehindContentView(R.layout.leftslidemenu);
        btnRightSlide = (View) findViewById(R.id.bt_slidesearch);
//        btn_search = (Button) findViewById(R.id.btn_search);
//        searchField=(EditText) findViewById(R.id.Edit1);
        //searchField.setEnabled(false);
        
        //mNav.getRightBehindView().setVisibility(mNav.getRightBehindView().GONE);
        btnRightSlide.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	mNav.openMenu();
            }
        });
        
        OCL_slideMenu_Search=new OnClickListener() 
        {
        	@SuppressWarnings("deprecation")
			@Override
        	public void onClick(View v) {
        		
        		mNav.close();
	        	Intent intent = new Intent(top, TransitionActivity.class);
	            startActivity(intent);
	            overridePendingTransition(0, 0);

        	}
        };
        
        btn_home=(View)findViewById(R.id.bt_home);
        btn_home.setOnClickListener(this);

        
        btn_banner=(View)findViewById(R.id.bt_banner);
        btn_banner.setOnClickListener(this);
        
        
        backPressCloseHandler = new BackPressCloseHandler(this);
        
        final View btn_me = (View) findViewById(R.id.bt_me);
        btn_me.setOnClickListener(new OnClickListener() {
        	public void onClick(View view){        		
        		Intent intent;
        		intent = new Intent(top, PopupMe.class);
            	startActivity(intent);
        	}
        }
        
        		);
        
        backPressCloseHandler = new BackPressCloseHandler(this);
       
    }

      
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch(v.getId())
		{
		case R.id.bt_home :
			if(top.getClass()==MainActivity.class) break;
			intent = new Intent(top, MainActivity.class);
        	startActivity(intent);
			break;
			
		case R.id.bt_banner :
			if(top.getClass()==BannerActivity.class) break;
			intent = new Intent(top, BannerActivity.class);
        	startActivity(intent);
        	overridePendingTransition(0, 0);
			break;

		}
	}

	public void onBackPressed() {
		//super.onBackPressed();
		backPressCloseHandler.onBackPressed(top);
	}

//	protected void onApplyThemeResource() {
//		// TODO Auto-generated method stub
//		
//	}
	protected void getStatusBarSize() {
	    Rect rectgle = new Rect();
	    Window window = getWindow();
	    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
	    int StatusBarHeight = rectgle.top;
	    int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
	    int TitleBarHeight = contentViewTop - StatusBarHeight;
	    heightS=StatusBarHeight;
	    heightA=TitleBarHeight;
	    Log.i("getHeight", "StatusBar Height= " + StatusBarHeight + " TitleBar Height = " + TitleBarHeight);
	}
	
}
